package com.yxuan.springboot.util;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DaysCalculateUtil {


    //计算两个日期间隔天数
    public static int differentDays(Date currentDate, Date expiryDate) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(currentDate);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(expiryDate);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    //对指定日期添加天数获得新日期
    public static Date targetDate(Date date,int year,int month,int day) throws ParseException {


//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
//        String str="20110823";
//        Date dt=sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
//        rightNow.add(Calendar.YEAR, -1);//日期减1年
//        rightNow.add(Calendar.MONTH, 3);//日期加3个月
//        rightNow.add(Calendar.DAY_OF_YEAR, 10);//日期加10天
        rightNow.add(Calendar.YEAR, year);//年加减
        rightNow.add(Calendar.MONTH, month);//月加减
        rightNow.add(Calendar.DAY_OF_YEAR, day);//天数加减
        Date newdate = rightNow.getTime();
//        String reStr = sdf.format(dt1);
//        System.out.println(reStr);
        return newdate;


    }


    /**
     * 计算两个日期间隔的小时数
     * @throws ParseException
     */
    public static double mytest15(Date fromdate,Date todate) throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(fromdate);
        String format1 = dateFormat.format(todate);
        Date startDate = dateFormat.parse(format);
        Date endDate = dateFormat.parse(format1);
        Integer datePoor = getDatePoor(startDate,endDate);
        BigDecimal b = new BigDecimal((double)datePoor/60);
        Double hour = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//第一个参数是保留小数的位数
        return hour;
    }

    public static Integer getDatePoor(Date startDate, Date endDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        Long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        Long mymint = diff/1000/60;
        int intValue = mymint.intValue();
        return intValue;
    }


}
